package controller

import (
	"gf-start-kit/app/middleware"
	"gf-start-kit/app/service"
	"gf-start-kit/library/base"
	"gf-start-kit/library/resp"
	"gf-start-kit/library/router"
	"github.com/gogf/gf/net/ghttp"
	"github.com/golang/glog"
)

type AuthGroupController struct {
	base.Controller
	service *service.AuthGroupService
}

func init() {
	ctl := new(AuthGroupController)
	router.New("auth_group", "system/auth_group", middleware.Auth).
		POST("/save", ctl.Save, router.Opts{
			OpenAuth: true,
		}).
		POST("/edit", ctl.Edit, router.Opts{
			OpenAuth: true,
		}).
		POST("/remove", ctl.Remove).
		GET("/get", ctl.Get).
		GET("/list", ctl.List, router.Opts{
			OpenAuth:      true,
			RouterLogName: "管理员列表",
		}).
		GET("/page", ctl.Page)
}

func (a *AuthGroupController) Save(r *ghttp.Request) {
	var req service.AuthGroupReq
	if err := r.Parse(&req); err != nil {
		resp.Error(r).SetError(err).Json()
	}
	_, err := a.service.Save(req)
	if err != nil {
		resp.Error(r).Json()
		glog.Error(err.Error())
	}
	resp.Success(r).Json()
}

func (a *AuthGroupController) Edit(r *ghttp.Request) {
	var req service.AuthGroupReq
	if err := r.Parse(&req); err != nil {
		resp.Error(r).SetError(err).Json()
	}
	_, err := a.service.Update(req)
	if err != nil {
		resp.Error(r).SetError(err).Json()
	}
	resp.Success(r).Json()
}

//无权限获取信息列表
func (a *AuthGroupController) List(r *ghttp.Request) {
	form := base.NewForm(r.GetQueryMap())
	list, err := a.service.List(&form)
	if err != nil {
		resp.Error(r).SetError(err).Json()
	}
	resp.Success(r).SetData(list).Json()
}

//分页列表
func (a *AuthGroupController) Page(r *ghttp.Request) {
	form := base.NewForm(r.GetQueryMap())
	list, err := a.service.Page(&form)
	if err != nil {
		resp.Error(r).SetError(err).Json()
	}
	resp.Success(r).SetData(resp.ListsData{Total: form.Total, Rows: list}).Json()
}
func (a *AuthGroupController) Remove(r *ghttp.Request) {
	id := r.GetInt64("id")
	_, err := a.service.Remove(id)
	if err != nil {
		resp.Error(r).SetError(err).Json()
	}
	resp.Success(r).Json()
}
func (a *AuthGroupController) Get(r *ghttp.Request) {
	id := r.GetInt64("id")
	one, err := a.service.Get(id)
	if err != nil {
		resp.Error(r).SetError(err).Json()
	}
	resp.Success(r).SetData(one).Json()
}
